package com.ef.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.StatusType;
import com.ef.dao.ClassFocusDao;
import com.ef.domain.ClassFocus;
import com.ef.service.ClassFocusService;
import com.ef.util.CustomExample;
import com.ef.util.PaginationSupport;

public class ClassFocusServiceImpl extends BaseServiceImpl implements
		ClassFocusService {

	private ClassFocusDao classFocusDao;

	public void delete(ClassFocus entity) {
		entity.setStatus(StatusType.INVALID);
		classFocusDao.update(entity);

	}

	public PaginationSupport<ClassFocus> findByExample(
			CustomExample<ClassFocus> example, int startIndex, int pageSize) {

		return classFocusDao.findPageByExample(example, startIndex, pageSize);
	}

	public List<ClassFocus> findALLValid() {
		DetachedCriteria criterion = DetachedCriteria
				.forClass(ClassFocus.class);
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		return classFocusDao.findAllByCriteria(criterion);
	}

	public ClassFocus findById(Long id) {

		return classFocusDao.findById(id);
	}

	public void saveOrUpdate(ClassFocus entity) {
		classFocusDao.clear();
		classFocusDao.saveOrUpdate(entity);
	}

	public ClassFocusDao getClassFocusDao() {
		return classFocusDao;
	}

	public void setClassFocusDao(ClassFocusDao classFocusDao) {
		this.classFocusDao = classFocusDao;
	}

	public List<ClassFocus> findALL() {
		DetachedCriteria criteria = DetachedCriteria.forClass(ClassFocus.class);
		criteria.add(Restrictions.eq("status", StatusType.VALID));
		return classFocusDao.findAllByCriteria(criteria);

	}

	public List<ClassFocus> findByName(String name, int max) {
		StringBuffer hql = new StringBuffer(
				"From ClassFocus where (lower(name) like '%");
		hql.append(name.trim().toLowerCase());
		hql.append("%') and status='VALID' ");
		return classFocusDao.findMultiAll(hql.toString(), 0, max);

		// DetachedCriteria criterion = DetachedCriteria
		// .forClass(ClassFocus.class);
		// criterion.add(Restrictions.or(Restrictions.like("name", name
		// .toLowerCase(), MatchMode.ANYWHERE), Restrictions.like("name",
		// name.toUpperCase(), MatchMode.ANYWHERE)));
		// criterion.add(Restrictions.eq("status", StatusType.VALID));
		// return classFocusDao.findAllByCriteria(criterion, max);
	}

	public List<ClassFocus> findByType(Long classtypeid) {
		DetachedCriteria criterion = DetachedCriteria
				.forClass(ClassFocus.class);
		criterion.createCriteria("focusType").add(
				Restrictions.eq("id", classtypeid));
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		return classFocusDao.findAllByCriteria(criterion);
	}

	public List<ClassFocus> findByNameAndType(String name, Long classtypeid,
			int max) {
		DetachedCriteria criterion = DetachedCriteria
				.forClass(ClassFocus.class);
		criterion.createCriteria("focusType").add(
				Restrictions.eq("id", classtypeid));
		criterion.add(Restrictions.like("name", name
				.trim(), MatchMode.ANYWHERE));
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		return classFocusDao.findAllByCriteria(criterion, max);
	}
}
